<template>
  <block v-if="initData">
    <ApplianceSettingsView :api-control="apiControl" :ota-helper="otaHelper" @to-param-settings="toParameterSettings" />
  </block>
</template>

<script setup lang="ts">
import ApplianceSettingsView, { type ApiControl, type ToParameterSettingsPayload } from './applianceSettingsView.vue'
import {
  getApplianceDetail,
  getAllSetting,
  getFeatures,
  getGuest,
  deregister,
  changeApplianceName,
  setSetting,
  setHomeApplianceSettings,
  setHVNHomeApplianceSettings,
  getAppSupportCommands,
  getSimpleSetting,
  getSettingGroups,
  getStatus,
  getSetting,
} from '@/api/appliance'
import { getProfileVersion, getFirmwareVersion } from '@/api/biwd'
import { renameRoomName } from '@/api/tt'
import { setUserAddition } from '@/api/account'
import { onLoad, onUnload } from '@dcloudio/uni-app'
import { createOtaHelper } from './ota/helpers/index'
import { toPage } from '@/utils/mpHelper'
import type { OTAHelper } from './ota/helpers/types'

const apiControl: ApiControl = {
  getApplianceDetail,
  getAllSetting,
  getFeatures,
  getGuest,
  deregister,
  changeApplianceName,
  setSetting,
  setHomeApplianceSettings,
  setHVNHomeApplianceSettings,
  getAppSupportCommands,
  getSimpleSetting,
  setUserAddition,
  renameRoomName,
  getSettingGroups,
  getStatus,
  getSetting,
  getProfileVersion,
  getFirmwareVersion,
}

const initData = ref(false)
let otaHelper: OTAHelper | null = null

onLoad((query: any) => {
  // 创建固件升级方法
  otaHelper = createOtaHelper({
    brand: query.brand,
    type: query.type,
    vib: query.vib,
    haId: query.haId,
    scene: 'setting',
  })

  nextTick(() => (initData.value = true))
})
onUnload(() => {
  otaHelper?.destroy()
})

/**
 * OTA升级
 */

/**
 * 跳转参数设置
 */
function toParameterSettings(payload: ToParameterSettingsPayload) {
  toPage('/pages/appliance/settings/parameterSettings', payload)
}
</script>
